package com.maduar.demo.controller;

import com.maduar.demo.User;
import com.maduar.demo.util.DemoHttpUtils;
import com.maduar.demo.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Description:
 *
 * @author danny.pang danny.pang@ebizprise.com
 * @date 2020-03-14 21:32
 */
@CrossOrigin
@Controller
@Validated
@Slf4j
public class JmeterParameterController {


    @GetMapping("getName")
    public HttpEntity<Result<String>> getName() {
        ResponseEntity.BodyBuilder builder = ResponseEntity.ok();

        return builder.body(Result.success("maduar"));
    }

    @GetMapping("getAge")
    public HttpEntity<Result<Integer>> getAge() {
        ResponseEntity.BodyBuilder builder = ResponseEntity.ok();

        return builder.body(Result.success(17));
    }

    @GetMapping("getYourAge")
    public HttpEntity<Result<Integer>> getYourAge(@RequestParam("age") int age, HttpServletRequest request) {

        String localIp = DemoHttpUtils.getLocalIp(request);
        String remoteAddrIp = DemoHttpUtils.getRemoteAddrIp(request);

        log.info("localIp: [{}], remoteAddrIp: [{}]", localIp, remoteAddrIp);

        ResponseEntity.BodyBuilder builder = ResponseEntity.ok();

        return builder.body(Result.success(age));
    }

    @GetMapping("getUser")
    public HttpEntity<Result<User>> getUser(@RequestParam("name") String name,
                                            @RequestParam("age") int age) {
        ResponseEntity.BodyBuilder builder = ResponseEntity.ok();

        User user = new User();
        user.setName(name);
        user.setAge(age);
        return builder.body(Result.success(user));
    }

    @PostMapping("addUser")
    public HttpEntity<Result<User>> addUser(@RequestBody @Valid User user, HttpServletRequest request) {
        ResponseEntity.BodyBuilder builder = ResponseEntity.ok();

        return builder.body(Result.success(user));
    }
}
